<?php
require_once('./config.php');
$service = new Amazon_SQS_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);

$request = array('QueueUrl' => $queueUrl);
$output = array();

$loopFlag = true;
try {

do{
	$imageCount++;
	$readResponse = $service->receiveMessage($request);
	if ($readResponse->isSetReceiveMessageResult()) {

		$receiveMessageResult = $readResponse->getReceiveMessageResult();
		$messageList = $receiveMessageResult->getMessage();

		if(!count($messageList)) $loopFlag = false;
		foreach ($messageList as $message) {

			if ($message->isSetReceiptHandle() && $message->isSetBody()) {
				$receiptHandle = $message->getReceiptHandle();
				$body = trim($message->getBody());
echo '<br> Message Body : ' . $body;
				$body = json_decode($body);

			# TODO code ;
			# condition
// 			$output
				$delFlag = true;

			} # if set message handle and set body
		} # for each

	} else {
		$loopFlag = false;
	}
	# Deleting the message from the queue

	if($delFlag) {
		$delRequest = array('ReceiptHandle' => $receiptHandle,'QueueUrl' => $queueUrl);
		$delResponse = $service->deleteMessage($delRequest);
	}

// 	if($imageCount >= 6) $loopFlag = false;
} while($loopFlag);

} catch (Amazon_SQS_Exception $ex) {
	echo("Caught Exception: " . $ex->getMessage() . "\n<br>");
	echo("Response Status Code: " . $ex->getStatusCode() . "\n<br>");
	echo("Error Code: " . $ex->getErrorCode() . "\n<br>");
	echo("Error Type: " . $ex->getErrorType() . "\n<br>");
	echo("Request ID: " . $ex->getRequestId() . "\n<br>");
	echo("XML: " . $ex->getXML() . "\n<br>");
}

echo '<br><br> Total image messages converted : ' . count($output) . '<br>';
// header('Content-type: application/json');
echo json_encode($output);

?>